package cn.xianshu.basic.controller;


import cn.xianshu.basic.util.AxiosResult;
import cn.xianshu.basic.util.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    /**
     * 上传接口
     * @param file
     * @return
     */
    @PostMapping
    public AxiosResult upload(@RequestPart(required = true,value = "file") MultipartFile file){
        try {
            // 获取图片后缀
            String filename = file.getOriginalFilename();
            String substring = filename.substring(filename.lastIndexOf(".") + 1);
            // 上传
            String path = FastDfsUtil.upload(file.getBytes(), substring);
            return AxiosResult.me().setResultObj(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统繁忙，请稍后重试。。。");
        }
    }

    /**
     * 删除操作
     * @return
     *  localhost:8080/fastDfs/1
     */
    @DeleteMapping
    public AxiosResult delete(@RequestParam String path){  // /group1/asdfjsa/safsldf/sadfs.jpg
        try {
            // 获取 / 之后的path
            String pathTmp = path.substring(1);
            // 获取 组
            String groupName = pathTmp.substring(0, pathTmp.indexOf("/"));
            // 获取图片名
            String remotePath = pathTmp.substring(pathTmp.indexOf("/") + 1);
            // 删除图片
            FastDfsUtil.delete(groupName,remotePath); // 拼接起来进行删除  组  路径下面的图片名
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统繁忙，请稍后重试。。。。");
        }

    }
}
